package com.rags.chess.move;

import com.rags.chess.base.Color;
import com.rags.chess.base.Coordinate;
import com.rags.chess.base.Piece;
import com.rags.chess.game.Game;
import com.rags.chess.pieces.Pawn;

public class EnPassant extends Move {
	public EnPassant(Game game, Coordinate start, Coordinate anEnd) {
		super(game, start, anEnd, null);
	}

	@Override
	protected String deriveNotation(Game game, Coordinate start, Coordinate end) {
		Piece startPiece = game.getPiece(start);
		if (!(startPiece instanceof Pawn))
			throw new IllegalArgumentException(String.format("Illegal en-passant attempted, piece not a pawn: %s", startPiece.getClass().getSimpleName()));
		if (startPiece.getColor().equals(Color.WHITE) && start.getNotation().charAt(1) != '5')
			throw new IllegalArgumentException("Illegal en-passant attempted, white pawn should be on 5th rank");
		if (startPiece.getColor().equals(Color.BLACK) && start.getNotation().charAt(1) != '4')
			throw new IllegalArgumentException("Illegal en-passant attempted, black pawn should be on 4th rank");
		if (Math.abs(start.getColumn()-end.getColumn()) != 1 || (startPiece.getColor().equals(Color.WHITE) && end.getNotation().charAt(1) != '6') || (startPiece.getColor().equals(Color.BLACK) && end.getNotation().charAt(1) != '3'))
			throw new IllegalArgumentException(String.format("Illegal pawn capture: %s to %s", start, end));
		Piece endPiece = game.getPiece(end);
		if (endPiece != null)
			throw new IllegalArgumentException(String.format("Illegal en-passant attempted, %s square is occupied by a %s %s", end, endPiece.getColor(), endPiece.getClass().getSimpleName()));
		
		Coordinate endPieceCoordinate = new Coordinate(String.format("%c%c", end.getNotation().charAt(0), start.getNotation().charAt(1)));
		endPiece = game.getPiece(endPieceCoordinate);
		if (endPiece == null)
			throw new IllegalArgumentException(String.format("%s is empty", endPieceCoordinate));
		if (!(endPiece instanceof Pawn))
			throw new IllegalArgumentException(String.format("%s is occupied by a %s %s", endPieceCoordinate, endPiece.getColor(), endPiece.getClass().getSimpleName()));
		
		Move lastMove = game.getLastMove();
		if (lastMove == null || !endPieceCoordinate.equals(lastMove.getEnd()))
			throw new IllegalArgumentException(String.format("Pawn on %s didn't move there on the last move", endPieceCoordinate));
		if ((endPiece.getColor().equals(Color.WHITE) && lastMove.getStart().getNotation().charAt(1) != '2') || (endPiece.getColor().equals(Color.BLACK) && lastMove.getStart().getNotation().charAt(1) != '7'))
			throw new IllegalArgumentException(String.format("Pawn on %s didn't move there from its starting square", endPieceCoordinate));
		
		return String.format("%sx%s", start.getNotation().charAt(0), end);
	}

	@Override
	protected void makeMove(Game game, Coordinate start, Coordinate end) {
		Piece p = game.getPiece(start);
		Coordinate toBeCapturedPieceCoordinate = new Coordinate(String.format("%c%c", end.getNotation().charAt(0), start.getNotation().charAt(1)));
		Piece toBeCaptured = game.getPiece(toBeCapturedPieceCoordinate); 
		game.removePiece(toBeCaptured);
		
		p.setPosition(end);
		game.getSquare(toBeCapturedPieceCoordinate).setPiece(null);
		game.getSquare(start).setPiece(null);
		game.getSquare(end).setPiece(p);
	}
}
